package com.alibaba.citrus.cr.order.open.api.facade.service.fulfillmentorder;

import com.alibaba.citrus.cr.order.open.api.facade.common.model.WmsOutBoundMessage;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrder;
import com.alibaba.citrus.cr.order.open.api.facade.transfer.CreateFulfillmentOrderReqFactory;
import com.alibaba.citrus.cr.unified.inventory.handler.FulfillmentOrderOpenApi;
import com.alibaba.cloud.retail.delivery.api.model.delivery.request.fulfillment.CreateFulfillmentOrderReq;
import com.alibaba.cloud.retail.delivery.api.model.delivery.request.fulfillment.SearchBcShipOrderReq;
import com.alibaba.cloud.retail.delivery.api.model.delivery.request.fulfillment.UpdateFulfillmentOrderFeaturesReq;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.Result;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.fulfillment.BcShipOrderSearchRes;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.fulfillment.FulfillmentGetRes;
import com.alibaba.cloud.retail.delivery.api.model.delivery.response.fulfillment.FulfillmentOrderResponse;
import com.alibaba.cloud.retail.delivery.api.service.bcfulfillment.BcShipOrderAbilityService;
import com.alibaba.cloud.retail.delivery.api.service.bcfulfillment.FulfillmentOrderAbilityService;
import com.alibaba.cloud.retail.wms.api.model.orders.common.WarehousingOrderDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crunifiedinventory.model.enums.FulfillmentTypeEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateOrderTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年02月02日 14:31
 **/
@Component
public class PlanTransferFulfillmentHandler extends TransferFulfillmentOrderHandler{
    private Log log = Log.getLogger(PlanTransferFulfillmentHandler.class);

    @Resource
    private CreateFulfillmentOrderReqFactory createFulfillmentOrderReqFactory;
    @DubboReference
    private FulfillmentOrderAbilityService fulfillmentOrderAbilityService;
    @DubboReference
    private BcShipOrderAbilityService bcShipOrderAbilityService;
    @Resource
    private FulfillmentOrderOpenApi fulfillmentOrderOpenApi;

    @Override
    public boolean support(TransferOrder transferOrder) {
        return AllocateOrderTypeEnum.PLAN.getType().toString().equals(transferOrder.getTransferOrderType())
                && supportFulfillmentType(transferOrder);
    }

    @Override
    public void handler(List<WarehousingOrderDTO> warehousingOrderDTOList, TransferOrder transferOrder, WmsOutBoundMessage message) {
        /**
         * 创建逆向履约单
         * 1.获取到出入库单号
         */
        if (idempotent(message)){
            log.info("PlanTransferFulfillmentHandler_createReverseOrder_idempotent:{}",JSON.toJSONString(message));
            return;
        }
        //根据出入库单查询履约单
        CreateFulfillmentOrderReq req = createFulfillmentOrderReqFactory.build(message, transferOrder);
        if (req == null){
            log.error("PlanTransferFulfillmentHandler_createReverseOrder_req_empty");
            return;
        }
        log.info("PlanTransferFulfillmentHandler_createReverseOrder_request:{}", JSON.toJSONString(req));
        Result<FulfillmentOrderResponse> reverseOrder = fulfillmentOrderOpenApi.createReverseOrder(req);
        log.info("PlanTransferFulfillmentHandler_createReverseOrder_result:{}", JSON.toJSONString(reverseOrder));
        if (reverseOrder != null && reverseOrder.isSuccess()) {
            UpdateFulfillmentOrderFeaturesReq featuresReq = createFulfillmentOrderReqFactory.build(req, reverseOrder.getResult());
            log.info("PlanTransferFulfillmentHandler_updateFulfillmentOrderFeatures_request:{}",JSON.toJSONString(featuresReq));
            Result featureRes = fulfillmentOrderAbilityService.updateFulfillmentOrderFeatures(featuresReq);
            log.info("PlanTransferFulfillmentHandler_updateFulfillmentOrderFeatures_result:{}",JSON.toJSONString(featureRes));
        }


    }

    @Override
    public boolean idempotent(WmsOutBoundMessage message) {
        List<FulfillmentGetRes> fulfillmentInfo = createFulfillmentOrderReqFactory.getFulfillmentInfo(message.getWmsOrderDTO().getWarehousingOrderId());
        log.info("PlanTransferFulfillmentHandler_createReverseOrder_idempotent_result:", JSON.toJSONString(fulfillmentInfo));
        return CollectionUtils.isNotEmpty(fulfillmentInfo);
    }

    /**
     * 根据出入库号或交货单号查询装运单号
     * @param outBoundOrderNo
     * @param deliveryNo
     * @return
     */
    public List<BcShipOrderSearchRes> getShipOrder(String outBoundOrderNo, String deliveryNo){
        SearchBcShipOrderReq req = new SearchBcShipOrderReq();
        req.setOutBoundOrderNo(outBoundOrderNo);
        req.setDeliveryNo(deliveryNo);
        com.alibaba.cloud.retail.delivery.api.model.delivery.response.Result<List<BcShipOrderSearchRes>> shipOrder = bcShipOrderAbilityService.searchShipOrder(req);
        log.info("CreateFulfillmentOrderReqFactory_searchShipOrder_result:{}",JSON.toJSONString(shipOrder));
        if (shipOrder == null || CollectionUtils.isNotEmpty(shipOrder.getResult())){
            return null;
        }
        return shipOrder.getResult();
    }

    @Override
    public boolean supportFulfillmentType(TransferOrder transferOrder) {
        return FulfillmentTypeEnum.CURRENT2PILOT.getValue().toString().equals(transferOrder.getFulfillmentTypeCode());
    }
}
